/**
 * created by 小卷毛, 2020-05-22 022
 * Copyright (c) 2020, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.kongqw.wechathelper.listener

import com.kongqw.wechathelper.WeChatBaseHelper
import com.kongqw.wechathelper.net.response.AccessTokenInfo
import com.kongqw.wechathelper.net.response.WeChatUserInfo

fun WeChatBaseHelper.auth(listener: _OnWeChatAuthLoginListener.() -> Unit) {
    val mListener = _OnWeChatAuthLoginListener()
    mListener.listener()
    authLogin(mListener)
}

@Suppress("ClassName")
open class _OnWeChatAuthLoginListener : OnWeChatAuthLoginListener {

    private var _onWeChatAuthLoginStart: (() -> Unit)? = null
    private var _onWeChatAuthLoginSuccess: ((AccessTokenInfo?, WeChatUserInfo?) -> Unit)? = null
    private var _onWeChatAuthLoginCancel: (() -> Unit)? = null
    private var _onWeChatAuthLoginAuthDenied: (() -> Unit)? = null
    private var _onWeChatAuthLoginError: ((Int?, String?) -> Unit)? = null

    fun onWeChatAuthLoginStart(listener: () -> Unit) {
        _onWeChatAuthLoginStart = listener
    }

    fun onWeChatAuthLoginSuccess(listener: (AccessTokenInfo?, WeChatUserInfo?) -> Unit) {
        _onWeChatAuthLoginSuccess = listener
    }

    fun onWeChatAuthLoginCancel(listener: () -> Unit) {
        _onWeChatAuthLoginCancel = listener
    }

    fun onWeChatAuthLoginAuthDenied(listener: () -> Unit) {
        _onWeChatAuthLoginAuthDenied = listener
    }

    fun onWeChatAuthLoginError(listener: (Int?, String?) -> Unit) {
        _onWeChatAuthLoginError = listener
    }

    override fun onWeChatAuthLoginStart() {
        _onWeChatAuthLoginStart?.invoke()
    }

    override fun onWeChatAuthLoginSuccess(
        accessTokenInfo: AccessTokenInfo?,
        weChatUserInfo: WeChatUserInfo?
    ) {
        _onWeChatAuthLoginSuccess?.invoke(accessTokenInfo, weChatUserInfo)
    }

    override fun onWeChatAuthLoginCancel() {
        _onWeChatAuthLoginCancel?.invoke()
    }

    override fun onWeChatAuthLoginAuthDenied() {
        _onWeChatAuthLoginAuthDenied?.invoke()
    }

    override fun onWeChatAuthLoginError(errorCode: Int?, errorMessage: String?) {
        _onWeChatAuthLoginError?.invoke(errorCode, errorMessage)
    }

}
